// index.js
const app = getApp();
const request = require('../../utils/request.js');
const util = require('../../utils/util.js');
const ApiRootUrl = app.globalData.REQUEST_IP_HEAD;

Page({

  /**
   * 页面的初始数据
   */
  data: {
        // 工具栏
        iconList: [{
          src: ApiRootUrl + '/res/img/miniApp/排队看诊.png',
          name: '预约看诊',
          url: '/pages/stuAppoint/stuAppoint'
        }, {
          src: ApiRootUrl + '/res/img/miniApp/看诊.png',
          name: '即时看诊',
          url: '/pages/goDiagnostic/goDiagnostic'
        }, {
          src: ApiRootUrl + '/res/img/miniApp/患者信息.png',
          name: '学生信息',
          url: '/pages/userStu/userStu'
        }, {
          src: ApiRootUrl + '/res/img/miniApp/医生&医生介绍.png',
          name: '科室医生',
          url: '/pages/departDr/departDr'
        }, {
          src: ApiRootUrl + '/res/img/miniApp/医用.png',
          name: '医疗用品',
          url: '/pages/medicine/medicine'
        }, {
          src: ApiRootUrl + '/res/img/miniApp/疾病.png',
          name: '疾病百科',
          url: '/pages/disease/disease'
        }, {
          src: ApiRootUrl + '/res/img/miniApp/运营数据统计.png',
          name: '数据统计',
          url: '/pages/statistics/statistics'
        },
        // {
        //   src: ApiRootUrl + '/res/img/miniApp/资讯管理.png',
        //   name: '通知管理',
        //   url: '/pages/essayOperate/essayOperate'
        // }
      ],
        chatList: [],
        defaultStuAvator: ApiRootUrl + "/res/img/avatar/学生头像.png"
  },

    /**
   * 点击菜单某项跳转到对应页面
   */
  operateTo(e){
    // console.log(e.currentTarget.dataset.url);
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

    /**
   * 请求获取聊天列表
   */
  getChatList(){
    var that = this;
    var userId = wx.getStorageSync('userInfo').id;
    request.get('/chat/getChatMsgList', {
      userId: userId,
    }).then((res) => {
      let list = res.data;
      for(let i=0; i<list.length;i++){
        let item = list[i];
        item.time = util.dealWithTime(item.time);
        list[i] = item;
      }
      that.setData({
        chatList: list
      })
      if (res.code != 200) {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
 
    })
  },

  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection =='left'){
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },

  /**
   * 跳转到聊天页面
   */
  toChat(e){
    const that = this;
    var you = this.data.chatList[e.currentTarget.dataset.index];
    wx.navigateTo({
      url: '/pages/chat/chat?youId='+you.friId+'&youName='+you.name+'&youUnit='+you.entName+'&youAvatar='+that.data.defaultStuAvator,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getChatList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getChatList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
